const util = require("../utils/util")
const db =wx.cloud.database();
const app = getApp()
Page({


    data: {
     nick:'',
     src:'',
    },
    
    onLoad: function (options) {

    },
    getuserinfo:function(){
        let that=this
        console.log("success")
        wx.getUserProfile({
          desc: '用于完善个人信息',
          success(res){
            app.globalData.userInfo = res.userInfo
            console.log(res.userInfo)
            that.setData({
                nick:res.userInfo.nickName,
                src:res.userInfo.avatarUrl
            })
          
           
          }
        })
    },


    addFriend(){
        console.log(app.globalData.userInfo)
        if(!app.globalData.userInfo){
          wx.showToast({
            icon:'none',
            title: '请登录',
          })
          return
        }
        
        var that = this;
    
        const _ = wx.cloud.database().command
        wx.cloud.database().collection("auto_reply_chatRecord").where({
            _openid:app.globalData.openid,
        }).get({
          success(ress){
            console.log(ress)
            if(ress.data.length==0){
              wx.cloud.database().collection('auto_reply_chatRecord').add({
                data:{
                  
                    //openid: app.globalData.openid,
                    avatarUrl: app.globalData.userInfo.avatarUrl,
                    nickName: app.globalData.userInfo.nickName,
          
                    record:[],//聊天记录
                    time:util.formatTime(new Date())
                },
                success(res){
                  console.log(res)
                  wx.navigateTo({
                    url: '../chat/chat?id=' + res._id,
                  })
                }
              })
            }else{
              wx.navigateTo({
                url: '../chat/chat?id=' + ress.data[0]._id,
              })
            }
          }
        })
    
        
      },
  


    
})